require([
    'jquery',
    'module/util',
    'module/popup',
    'global',
    'datatables.fixedcolumns',
    'jquery.chosen',
	'requirejs/domready!'],
function($, util, popup, g) {
	var vmf_table;
	
	$("#line-select").chosen({
		width: "100%",
		no_results_text: "没有发现记录匹配"
	}); 
	$('#edit-vm-modal').on('show.bs.modal', function(){
		var obj = $(this).data('obj');
		vmf_table.clear().draw();
		if(!obj){
			$(this).find('.modal-header .action').text('添加');
		}else{
			$(this).find('.modal-header .action').text('编辑');
			vmf_table.rows.add(obj.features).draw();
		}
		obj = obj || {};
		$(this).find('form input, form select').each(function(){
			if($(this).attr('name')){
				$(this).val(obj[[$(this).attr('name')]]);
			}
		});
		$(this).find('form select').trigger("chosen:updated");
	}).on('click', '.btn-primary', function(){
		var dto = {};
		$('#edit-vm-modal').find('form input, form select').each(function(){
			if($(this).attr('name')){
				dto[$(this).attr('name')] = $(this).val();
			}
		});
		dto.features = [];
		vmf_table.rows().data().each(function(d){
			dto.features.push({
				id: d.id,
				name: (d.name || '').replace(/\s/gi,''),
				categoryNumber: d.categoryNumber,
				changed: d.changed
			});
			
		});
		
		$.ajax({
		  type: 'post',
		  dataType: 'json',
		  url: g.context+ '/admin/vehicle_model',
		  data: JSON.stringify(dto),
		  contentType: 'application/json; charset=utf-8',
		  headers:{
			  'x-form-id': 'edit-vm-modal'
		  }
	    }).done(function(result){
	    	if(result.code == 'ACK'){
		    	$('#edit-vm-modal').modal('hide');
		    	$(window).trigger('ok.xt.modal', ['#edit-vm-modal']);
	    	}
	    });
	});
	// 添加事件监听
	$('#edit-vm-modal').on('click', '#vmf-table_wrapper .toolbar .btn.add', function(){
		$(window).trigger('show.xt.modal', ['#edit-vmf-modal']);
	}).on('click', '#vmf-table_wrapper .toolbar .btn.remove', function(){
		vmf_table.row('.selected').remove().draw();
	}).on('click', '#vmf-table_wrapper .toolbar .btn.edit', function(){
		var d = vmf_table.row('.selected').data();
		$(window).trigger('show.xt.modal', ['#edit-vmf-modal', d]);
	});
	$(window).on('ok.xt.modal', function(e, modalId, d){
		if(modalId == '#edit-vmf-modal'){
			if(!d.id){
				vmf_table.row.add(d).draw();
			}else{
				vmf_table.row(function(idx, data, node){
					return d.id == data.id;
				}).data(d).draw();
			}
		}
	});
	// 渲染VMF表格
	$('#vmf-table').on('click', 'tbody td', function(){
		var dt = $('#vmf-table').dataTable().api();
		var idx = dt.cell(this).index();
		if(!idx) return;
		var row = dt.row(idx.row).data();
		$(this).parents('tbody').find('tr.selected').removeClass('selected');
		$(this).parent().addClass('selected');
	}).on('init.dt', function(){
		$('#vmf-table_wrapper .toolbar').html(
				'<div class="btn-group" role="group">'+
				    '<button type="button" class="btn btn-default add"><i class="fa fa-plus"></i></button>'+
				    '<button type="button" class="btn btn-default remove"><i class="fa fa-times"></i></button>'+
				    '<button type="button" class="btn btn-default edit"><i class="fa fa-edit"></i></button>'+
				'</div>');
    }).on('draw.dt', function(){
    	if(vmf_table){
    		vmf_table.columns.adjust();
    	}
    }).dataTable( {
    	language: {
            url: g.context + '/assets/js/chinese.json'
        },
        dom: '<"toolbar">rt<"row"<"col-md-4"i><"col-md-8"p>>',
	    columnDefs: [ 
	     	{ targets : ['_all'], defaultContent: '&nbsp;', searchable : false }
	    ],
	    columns: [
	        { data: 'categoryNumber'},
	        { data: 'name'},
	        { data: 'updatedAt'}
	    ]
    });
	vmf_table = $('#vmf-table').dataTable().api();
});